//
// Created by Administrator on 2021/5/12.
//
#include <vector>
#include <iostream>

using namespace std;

class Solution {
public:
    vector<int> xorQueries(vector<int> &arr, vector<vector<int>> &queries) {
        auto n = arr.size();
        vector<int> prefix(n, 0);
        int temp = 0;
        for (int i = 0; i < n; ++i) {
            temp ^= arr[i];
            prefix[i] = temp;
        }
        vector<int> ans(queries.size(), 0);
        for (int i = 0; i < queries.size(); ++i) {
            int left = queries[i][0];
            int right = queries[i][1];
            if (left == 0 ) ans[i] = prefix[right];
            else ans[i] = prefix[right] ^ prefix[left-1];
        }
        return ans;
    }
};
class Solution2 { // 题解 思路一样
public:
    vector<int> xorQueries(vector<int>& arr, vector<vector<int>>& queries) {
        auto n = arr.size();
        vector<int> xors(n + 1);
        for (int i = 0; i < n; i++) {
            xors[i + 1] = xors[i] ^ arr[i];
        }
        auto m = queries.size();
        vector<int> ans(m);
        for (int i = 0; i < m; i++) {
            ans[i] = xors[queries[i][0]] ^ xors[queries[i][1] + 1];
        }
        return ans;
    }
};

int main() {
    vector<int> arr{1, 3, 4, 8};
    vector<vector<int>> query{
            {0, 1},
            {1, 2},
            {0, 3},
            {3, 3}};
    Solution sol;
    auto ans = sol.xorQueries(arr, query);
    for (const auto &x : ans) cout << x << endl;
    return 0;
}